package com.lqian.simplestructure.tree.binarytree;

public class Node {
    //节点的权
    int value;
    //左儿子
    Node leftNode;
    //右儿子
    Node rightNode;

    public Node(int _value){
        this.value = _value;
    }

    public Node getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(Node leftNode) {
        this.leftNode = leftNode;
    }

    public Node getRightNode() {
        return rightNode;
    }

    public void setRightNode(Node rightNode) {
        this.rightNode = rightNode;
    }

    //前序遍历
    public void frontShow() {
        //先遍历当前节点的权
        System.out.println(value);

        if (leftNode != null){
            leftNode.frontShow();
        }

        if (rightNode != null){
            rightNode.frontShow();
        }

    }

    //中序遍历
    public void midShow() {
        if (leftNode != null){
            leftNode.midShow();
        }

        System.out.println(this.value);

        if (rightNode != null){
            rightNode.midShow();
        }
    }

    public void afterShow() {

        if (leftNode != null){
            leftNode.afterShow();
        }

        if (rightNode !=null){
            rightNode.afterShow();
        }

        System.out.println(this.value);
    }

    //前序查找
    public Node frontSearch(int value) {
        Node target = null;
        //对比当前节点
        if (this.value == value){
            return this;
        //当前节点不是要查找的值
        }else{
            //查找左儿子
            if (leftNode!=null){
                //有可能可以查到，也有可能查不到，查不到的话，target仍然为null
                target = leftNode.frontSearch(value);
            }
            if (target != null){
                return target;
            }
            //查找右儿子
            if (rightNode !=null){
                target = rightNode.frontSearch(value);
            }
        }
        return target;
    }

    //中序查找
    public Node midSearch(int value) {
        Node target =null;

        if (leftNode != null){
            target = leftNode.midSearch(value);
        }

        if (this.value == value){
            return this;
        }
        if (target != null){
            return target;
        }else{
            if (rightNode !=null){
                target = rightNode.midSearch(value);
            }
        }



        return target;
    }

    public Node afterSearch(int value) {
        Node target =null;

        if (leftNode != null){
            target = leftNode.afterSearch(value);
        }
        if (rightNode !=null){
            target = rightNode.midSearch(value);
        }
        if (target != null){
            return target;
        }
        if (this.value == value){
            return this;
        }
        return target;
    }

    //删除一颗子树
    public void delete(int value) {
        Node parent = this;

        //判断左儿子
        if (parent.leftNode != null && parent.leftNode.value == value){
            parent.leftNode = null;
            return;
        }
        //判断右儿子
        if (parent.rightNode != null && parent.rightNode.value == value){
            parent.rightNode = null;
            return;
        }

        //递归检索并删除左儿子
        parent = leftNode;
        if (parent != null){
            parent.delete(value);
        }

        //递归检索并删除右儿子
        parent = rightNode;
        if (parent != null){
            parent.delete(value);
        }


    }
}
